/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import simplemetrosystem.model.charlanguage.NameMap;

/**
 * 关于窗体，显示软件信息
 * @author Eric Qian
 */
public class AboutFrame extends JFrame {

    public AboutFrame(MetroFrame frame) {
        super();
        this.frame = frame;
        try {
            BufferedImage icon = ImageIO.read(new File("icon/about.gif"));
            this.setIconImage(icon);
        } catch (IOException ex) {
            Logger.getLogger(AboutFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        nameMap = NameMap.getInstance();
        //用户窗体大小可调
        this.setResizable(false);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //加载所有窗体组件
        loadComponents();
        pack();

        Toolkit theKit = this.getToolkit(); // Get the window toolkit

        Dimension wndSize = theKit.getScreenSize(); // Get screen size

        //设置窗体默认大小
        this.setBounds((int) (wndSize.getWidth() - FRAME_WIDTH) / 2,
                (int) (wndSize.getHeight() - FRAME_HEIGHT) / 2, FRAME_WIDTH, FRAME_HEIGHT);
        this.setVisible(true);
    }

    private void loadComponents() {

        add(new AboutPane(), BorderLayout.CENTER);
        add(getCloseButton(), BorderLayout.SOUTH);
    }

    private JButton getCloseButton() {
        if (closeButton == null) {
            closeButton = new JButton();
            closeButton.setText(nameMap.getName("Close"));
            closeButton.addActionListener(new CloseAboutActionListener(this));
        }
        return closeButton;
    }
    private MetroFrame frame;
    private NameMap nameMap;
    private JButton closeButton;
    public static final int FRAME_WIDTH = 400;
    public static final int FRAME_HEIGHT = 300;
}

class AboutPane extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        try {
            super.paintComponent(g);
            BufferedImage back = ImageIO.read(new File("icon/About.jpg"));
            g.drawImage(back, 0, 0, this);
        } catch (IOException ex) {
            Logger.getLogger(AboutPane.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class CloseAboutActionListener implements ActionListener {

    public CloseAboutActionListener(AboutFrame aboutFrame) {
        this.aboutFrame = aboutFrame;
    }

    public void actionPerformed(ActionEvent e) {
        aboutFrame.setVisible(false);
    }
    private AboutFrame aboutFrame;
}